package com.cari.oa.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.cari.oa.domain.CommentEntity;
import com.cari.oa.domain.SoftBugEntity;
import com.cari.oa.domain.SoftProjectEntity;
import com.cari.oa.domain.SysDictItemEntity;
import com.cari.oa.domain.SysUserEntity;
import com.cari.oa.service.CommentService;
import com.cari.oa.service.SoftBugService;
import com.cari.oa.service.SoftProjectService;
import com.cari.oa.service.SysDictItemService;
import com.cari.oa.service.SysUserService;
import com.cari.oa.util.StrUtils;
import com.cari.oa.util.Utils;

/**
* Title: SoftBugCommentController
* Description:评论Control
* Company: cari 
* @author  hjx 
* @date    2013-2-16
*/
@Controller
@SessionAttributes({"username","usercnname"})
public class SoftBugCommentController  {

	@Resource
	private CommentService commentService;
	@Resource
	private SoftProjectService softProjectService;
	@Resource
	private SoftBugService softBugService;
	@Resource
	private SysDictItemService sysDictItemService;
	@Resource
	private SysUserService sysUserService;
	
	public static final String MODULE_NAME = "BUG_COMMENT_MANAGE";
	
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(df, false));
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    }
	
    @RequestMapping("soft/bug/bugcomment.do")
    public String list(@ModelAttribute("username") String username,String bugId,String projectId,String qs, ModelMap modelMap) {
        List<CommentEntity> commentList = commentService.getListByObjID(bugId, CommentService.CMT_BUG);
        List<Map<String, String>> userList = softProjectService.getUserByProjectId(projectId,"0002");
        SoftBugEntity  bug = softBugService.getById(bugId);
        List<SysDictItemEntity> itemList = sysDictItemService.getItemsByGroupName("bugStatus");
        List<Map<String,String>> selectedUserList = sysUserService.getUserAndRole(projectId); 
        List<String> types = new ArrayList<String>();
        SoftProjectEntity   project = softProjectService.getById(projectId);
	 	if(StrUtils.isNotEmpty(project.getProjectManager()) && project.getProjectManager().equals(username))
	    {
	       types.add("3");
	    }else{
	        for(Map<String,String> map : selectedUserList)
	        {
	        	if(map.get("enname").equals(username))
	        	{
	        		types.add(map.get("type"));
	        	}
	        }    
	    }
        modelMap.addAttribute("itemList",itemList);
        modelMap.addAttribute("bug",bug);
        modelMap.addAttribute("types",types);
        modelMap.addAttribute("qs",qs);
        modelMap.addAttribute("currentStatus", bug.getStatus());
        modelMap.addAttribute("commentList", commentList);
        modelMap.addAttribute("userList", userList);
        return "soft/bug/comment";
    }
    
    @SuppressWarnings("static-access")
	@RequestMapping(value = "soft/bug/commentinsert.do")
    @ResponseBody
    public String postInsert(@ModelAttribute("username") String username,@ModelAttribute("usercnname") String usercnname,String status,String expurgator, @Valid CommentEntity commentEntity, BindingResult result) {
        if(result.hasFieldErrors())
            return Utils.errorJsonResp(result.getFieldError().getDefaultMessage());
        commentEntity.setObjectType(commentService.CMT_BUG);
        commentEntity.setUserEnname(username);
        commentEntity.setUserCnname(usercnname);
        commentService.insert(commentEntity);
        
        SoftBugEntity bug = softBugService.getById(commentEntity.getObjectId());
        bug.setStatus(status);
        SysUserEntity user = sysUserService.getByEnname(expurgator);
        bug.setExpurgator(expurgator);
        bug.setExpurgatorCn(user.getCnname());
        softBugService.update(bug);
        SysOperLogController.addOperLog(username, MODULE_NAME, "新增评论" + commentEntity.getContent());
        return Utils.normalJsonResp();
    }
}
